import { useState } from 'react';
import { useMutation } from 'react-query';
import { request } from '@/request';
import { EJobType, TJob, EStatus } from '@/types';
import { JobEditor } from '@/components';

const defaultData = {
   company: '',
   position: '',
   location: '',
   status: EStatus.PENDING,
   jobType: EJobType.FULL_TIME,
};

const JobAdd = () => {
   const [resetKey, setResetKey] = useState('reset');
   const addJob = useMutation(
      (job: TJob) =>
         request<TJob>('/jobs', {
            method: 'POST',
            data: job,
            showSuccess: true,
         }),
      {
         onSettled() {
            setResetKey(String(new Date().getTime()));
         },
      }
   );

   const handleSubmit = (data: TJob) => {
      addJob.mutate({
         company: data.company,
         position: data.position,
         location: data.location,
         status: data.status,
         jobType: data.jobType,
      });
   };

   return (
      <JobEditor onSubmit={handleSubmit} data={defaultData} key={resetKey} />
   );
};

export default JobAdd;
